package org.sam.swing.search;

import java.awt.Component;
import java.awt.Font;
import java.util.Map;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.table.TableModel;

import org.jdesktop.swingx.JXFindPanel;
import org.jdesktop.swingx.search.Searchable;
import org.sam.swing.JSComboBox;
import org.sam.swing.table.JSTable;

/**
 * 扩展的检索Panel
 * 
 * @author sam
 *
 */
public class JSFindPanel extends JXFindPanel {

	private static final long serialVersionUID = 2227928110713520833L;

	/**
	 * 替换掉文本
	 */
	public static final String COMMAND_REPLACE = "替换";

	/**
	 * 字段下拉列表
	 */
	protected JSComboBox<Integer, String> ddlbFields;

	/**
	 * 文本替换
	 */
	protected JTextField txtReplace;

	/**
	 * 应用选中行
	 */
	protected JCheckBox cbxSelectedRows;

	/**
	 * 正则表达式
	 */
	protected JCheckBox cbxPattern;

	/**
	 * 用来显示检索结果的
	 */
	protected JLabel lblMessage;

	/**
	 * 替换按钮
	 */
	protected JButton btnReplace;

	/**
	 * 当前可操作的列集合
	 */
	protected Map<Integer, String> colums;

	/**
	 * 当前可操作的列集合
	 * 
	 * @return
	 */
	public Map<Integer, String> getColums() {
		return colums;
	}

	/**
	 * 注入的table对象
	 */
	protected JSTable table;

	/**
	 * 注入的table对象
	 * 
	 * @return
	 */
	public JSTable getTable() {
		return table;
	}

	/**
	 * 注入的table对象
	 * 
	 * @param table
	 */
	public void setTable(JSTable table) {
		this.table = table;
	}

	/**
	 * 当前可操作的列集合
	 * 
	 * @param colums
	 */
	public void setColums(Map<Integer, String> colums) {
		this.colums = colums;
		if (this.ddlbFields != null && this.colums != null) {
			this.ddlbFields.removeAllItems();
			this.ddlbFields.addAll(colums);
		}
	}

	/**
	 * 扩展的检索Panel
	 */
	public JSFindPanel() {
		super();
	}

	/**
	 * 扩展的检索Panel
	 * 
	 * @param searchable
	 */
	public JSFindPanel(Searchable searchable) {
		super(searchable);
	}

	/**
	 * 扩展的检索Panel 并初始化下拉列表数据
	 * 
	 * @param searchable
	 *            检索对象
	 * @param colums
	 *            列集合对象
	 */
	public JSFindPanel(Searchable searchable, Map<Integer, String> colums) {
		super(searchable);
	}

	/**
	 * 初始化控件显示
	 */
	@Override
	protected void initComponents() {
		super.initComponents();
		this.ddlbFields = new JSComboBox<>();
		if (this.colums != null && !this.colums.isEmpty()) {
			this.ddlbFields.addAll(this.colums);
		}

		txtReplace = new JTextField();
		cbxSelectedRows = new JCheckBox("仅应用于选择的对象");
		cbxPattern = new JCheckBox("正则表达式");
		lblMessage = new JLabel("   ");
		btnReplace = new JButton("替换");
		btnReplace.addActionListener(e -> {
			// 直接替换
			int index = this.getPatternModel().getFoundIndex();
			if (index < 0) {
				JOptionPane.showMessageDialog(this, "请先检索到要替换的数据行");
				return;
			}
			index = index - 1;
			
			if (this.getTable() == null)
				return;

			TableModel model = this.getTable().getModel();
			Object v = model.getValueAt(index, this.ddlbFields.getValue());
			String txt = v.toString().replaceAll(searchField.getText(), txtReplace.getText());
			model.setValueAt(txt, index, this.ddlbFields.getValue());
		});
	}

	/**
	 * {@inheritDoc} 查找消息
	 */
	@Override
	protected void showFoundMessage() {
		// int index = 0;
		// int iCount = 0;
		//
		// while(index >= 0){
		// index = this.searchable.search(searchField.getText(), index);
		// if (index >= 0)
		// iCount++;
		// }
		//
		// lblMessage.setText("共找到:" + iCount);
	}

	/**
	 * 生成布局
	 */
	@Override
	protected void build() {

		Font font = new Font("宋体", Font.PLAIN, 12);
		Font sfont = new Font("宋体", Font.PLAIN, 6);
		Box lBox = new Box(BoxLayout.PAGE_AXIS);
		JLabel jLabel = new JLabel("属性:");
		jLabel.setFont(font);
		lBox.add(jLabel);

		JLabel jLabe0 = new JLabel("   ");
		jLabe0.setFont(sfont);
		lBox.add(jLabe0);

		JLabel jLabel2 = new JLabel("查找:");
		jLabel2.setFont(font);
		lBox.add(jLabel2);

		JLabel jLabe01 = new JLabel("   ");
		jLabe01.setFont(sfont);
		lBox.add(jLabe01);

		JLabel jLabel3 = new JLabel("替换:");
		jLabel3.setFont(font);
		lBox.add(jLabel3);
		lBox.setAlignmentY(Component.TOP_ALIGNMENT);

		Box rBox = new Box(BoxLayout.PAGE_AXIS);
		rBox.add(ddlbFields);
		rBox.add(searchField);
		rBox.add(txtReplace);
//		rBox.add(matchCheck);
//		rBox.add(wrapCheck);
//		rBox.add(backCheck);
		rBox.add(cbxSelectedRows);
		rBox.add(cbxPattern);
		rBox.add(lblMessage);

		rBox.setAlignmentY(Component.TOP_ALIGNMENT);

		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));

		add(lBox);
		add(rBox);
	}
}
